<?php

class App_Methods_Data_Application {
	
	public $id;
	public $name;
	public $description;
	public $ico;
	public $icoType;
	public $url;
	/**
	 * 
	 * @var App_Methods_Data_UserPulpit|null
	 */
	public $userPulpit = null;
	/**
	 * 
	 * @var App_Methods_Data_ApplicationData[]
	 */
	public $data = array();
	/**
	 *
	 * @var App_Methods_Data_ApplicationLink[]
	 */
	public $links = array();
	
	public function __construct(Applications $application, $withData=true) {
		$userPulpit = $application->getUserPulpits()->getFirst();
		$currentSet = null;
		foreach ($application->getApplicationDataSetss() as $applicationDataSet) {
			if ($currentSet == null) {
				$currentSet = $applicationDataSet;
				continue;
			}
			if ($currentSet->getLastModified() < $applicationDataSet->getLastModified())
				$currentSet = $applicationDataSet;
		}
		$lang = Core_Core::getInstance()->getApp()->lang;
		if ($lang == 'en') {
			$this->name = $currentSet->getNameEn();
			$this->description = $currentSet->getDescriptionEn();
		}
		else {
			$this->name = $currentSet->getName();
			$this->description = $currentSet->getDescription();
		}
		$this->id = $application->getId();
		$this->ico = $currentSet->getIco();
		$this->icoType = $currentSet->getIcoType();
		$this->url = $currentSet->getUrl();
		if ($userPulpit instanceof UserPulpit) {
			$this->userPulpit = new App_Methods_Data_UserPulpit($userPulpit);
		}
		if ($withData) {
			foreach ($currentSet->getApplicationDataDefss() as $def) {
				if ($def->getType() == ApplicationDataDefs::TYPE_INFO)
					$this->data[] = new App_Methods_Data_ApplicationData($def);
				else if ($def->getType() == ApplicationDataDefs::TYPE_LINK)
					$this->links[] = new App_Methods_Data_ApplicationLink($def);
			}
		}
	}
	
}